package com.twu.biblioteca.DataBase;

import java.util.ArrayList;

public class ListOfItems {
    private ArrayList<Item> itemList;

    public ListOfItems() {
        itemList = new ArrayList<Item>();
        itemList.add(new Book("book1", 1));
        itemList.add(new Book("book1", 2));
        itemList.add(new Book("book2", 3));
        itemList.add(new Book("book3", 4));
        itemList.add(new Book("book4", 5));
        itemList.add(new Movie(1, "One", 2011, "Tom", "Unrated"));
        itemList.add(new Movie(2, "Two", 2013, "Sam", "2"));
        itemList.add(new Movie(3, "Three", 2012, "Tim", "8"));
        itemList.add(new Movie(4, "Three", 2012, "Tim", "8"));
    }

    public ArrayList<Book> getBooks() {
        ArrayList<Book> books = new ArrayList<Book>();
        for(Item item : itemList){
            if (isBook(item)){
                books.add((Book)item);
            }
        }
        return books;
    }

    private boolean isBook(Item item) {
        return item.getItemType() == ItemType.Book;
    }

    public void printBookList() {
        System.out.println("There are books which can be check-out.");

        System.out.println("SerialNumber" + " " + "BookName");
        for (Item item : itemList){
            if(isBook(item) && !item.isCheckOut){
                System.out.println("    " + item.getSerialNumber() + "        " + item.getName());
            }
        }
    }

    public Item getBySerialNumber(int serialNumber, ItemType itemType) {
        for(Item item : itemList){
            if(item.getSerialNumber() == serialNumber && item.getItemType() == itemType){
                return item;
            }
        }
        System.out.println("SerialNumber is incorrect!");
        return null;
    }

    public void printMovieList() {
        System.out.println("There are movies which can be check-out.");
        System.out.println("SerialNumber" + " " + "name" + " " + "Director" +" " + "Year" + " " + "Score");
        for (Item item : itemList){
            if(!isBook(item) && !item.isCheckOut){
                Movie movie = (Movie) item;
                System.out.println("    " + movie.getSerialNumber() + "        " + movie.getName()
                + "     " + movie.getDirector() + "     " + movie.getYear() + "     " + movie.getScore());
            }
        }
    }
}
